package com.lzh.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzh.eduservice.entity.EduCourse;
import com.lzh.eduservice.entity.vo.CourseInfoForm;
import com.lzh.eduservice.entity.vo.CoursePublishVo;
import com.lzh.eduservice.entity.vo.CourseQuery;
import com.lzh.eduservice.service.EduCourseService;
import com.lzh.commonutils.R;
import org.springframework.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lzh
 * @since 2020-12-01
 */
@Api(description="课程管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/eduservice/edu-course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "新增课程")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
                               @RequestBody CourseInfoForm courseInfoForm) {

        String id = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId",id);
    }

    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoForm courseInfo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo",courseInfo);
    }

    @PostMapping("/updateCourse")
    public R updateCourse(@RequestBody CourseInfoForm courseInfoForm) {
        courseService.updateCourseInfo(courseInfoForm);
        return R.ok();
    }

    @GetMapping("/getPublishInfo/{courseId}")
    public R getPublishInfo(@PathVariable String courseId) {
        CoursePublishVo publishInfo = courseService.getPublishInfo(courseId);
        return R.ok().data("PublishInfo",publishInfo);
    }

    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    @GetMapping("/getCourseList/{page}/{limit}")
    public R getCourseList(@ApiParam(name = "page", value = "当前页码", required = true)
                               @PathVariable Long page,

                           @ApiParam(name = "limit", value = "每页记录数", required = true)
                               @PathVariable Long limit,

                           @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                                       CourseQuery courseQuery){
        Page<EduCourse> pageCourse = new Page<>(page, limit);

        courseService.pageQuery(pageCourse, courseQuery);

        long total = pageCourse.getTotal();
        List<EduCourse> records = pageCourse.getRecords();
        return R.ok().data("total",total).data("rows",records);

//        List<EduCourse> eduCourseList = courseService.list(null);
//        return R.ok().data("courseList",eduCourseList);
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        courseService.removeCourse(courseId);
        return R.ok();
    }
}

